package sel.me.popular.library.learning.disruptor.event;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * 使用Disruptor的3.0版本，添加了更丰富的Lambda风格的API，以帮助开发人员将这种复杂性封装在Ring Buffer中
 * ，因此3.0之后发布消息的首选方法是通过API的Event Publisher / Event Translator部分
 *
 * @author shenenlu 2021年05月14日 下午14:06:53
 */
public class LongEventProducerWithTranslator {

    private final RingBuffer<LongEvent> ringBuffer;


    private static final EventTranslatorOneArg<LongEvent, ByteBuffer> TRANSLATOR =
            (event, sequence, bb) -> event.set(bb.getLong(0));

    public LongEventProducerWithTranslator(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }



    public void onData(ByteBuffer bb) {
        ringBuffer.publishEvent(TRANSLATOR, bb);
    }

}
